﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;

namespace RegExExercise
{
    class RegularExpressionsExercise
    {

        #region WordEncounterFinder

        public static void PrintEncountersOfAWord(string text,string word)
        {
            string templateString = String.Format(@"[\w]*({0})+[\w]*", word.ToLower());
            Regex template = new Regex(templateString);
            MatchCollection encounters = template.Matches(text.ToLower());

            foreach (Match match in encounters)
            {
                Console.WriteLine(text.Substring(match.Index, match.Length) + "\t" + match.Index);
            }
        }

        #endregion

        #region RealNumberValidator

        public static bool IsRealNumber(string number)
        {
            string patern = @"\A(-)?(0|[1-9][0-9]*)(\.\d+)?\Z";
            Regex template = new Regex(patern);

            return template.IsMatch(number);
        }

        #endregion

        #region EmailExtractor

        public static MatchCollection ExtractEmails(string text)
        {
            string patern = @"[\w]+\@{1}([\w]+\.)+(\w{2,4}){1}";
            Regex template = new Regex(patern);

            return template.Matches(text);
        }

        public static void PrintMatchedEmails(string text)
        {
            MatchCollection matches = ExtractEmails(text);

            foreach (Match match in matches)
            {
                Console.WriteLine(match.Value);
            }
        }

        public static bool IsEmail(string text)
        {
            string patern = @"\A[\w]+\@{1}([\w]+\.)+(\w{2,4}){1}\Z";
            Regex template = new Regex(patern);

            return template.IsMatch(text);
        }

        #endregion

        #region URLextractor

        public static MatchCollection ExtractUrls(string text)
        {
            string patern = @"(http://)?(www.){1}(\w+\.)+((\w){2,4}){1}";
            Regex template = new Regex(patern);

            return template.Matches(text);
        }

        public static void PrintMatchedURLs(string text)
        {
            MatchCollection matches = ExtractUrls(text);

            foreach (Match match in matches)
            {
                Console.WriteLine(match.Value);
            }
        }

        public static bool IsUrl(string line)
        {
            string patern = @"\A(http://)?(www.){1}(\w+\.)+((\w){2,4}){1}\Z";
            Regex template = new Regex(patern);

            return template.IsMatch(line);
        }

        #endregion

        #region URLparser

        public static Dictionary<string, string> ParseURL(string url)
        {
            return null;
        }

        #endregion

        static void Main(string[] args)
        {

            string text = @"az sum si www.por.com i taka si e mamka mu http://www.umri.wa.kreten.com
                        principno pone de da go ea u www.por.org to vse pak vsi4ko e ok nali;";

            PrintMatchedURLs(text);
        }
    }
}
